package cn.mengfly.testnrz.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.util.Date;
import java.util.Scanner;

public class UDPClient {

    public void send() throws IOException {
        DatagramChannel channel = DatagramChannel.open();
        channel.configureBlocking(false);

        ByteBuffer buffer= ByteBuffer.allocate(1024);
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要发送的内容");
        while (scanner.hasNext()) {
            String next = scanner.next();
            buffer.put((new Date()+ " >>" + next).getBytes());
            buffer.flip();
            channel.send(buffer, new InetSocketAddress(NioDemoConfig.SOCKET_IP, NioDemoConfig.SOCKET_PORT));
            buffer.clear();
        }
        channel.close();
    }

    public static void main(String[] args) throws IOException{
        new UDPClient().send();
    }
}
